package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.TableEntity;
import cn.com.assistant.mapper.TableMapper;
import cn.com.assistant.service.TableService;
import cn.com.assistant.utils.ExcelDataVo;
import cn.com.assistant.utils.PackExcelSheetsDataUtil;
import cn.com.assistant.utils.RequestUtil;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping("/table")
public class TableController {
 
    @Autowired
    TableService tableService;

    @Autowired
    TableMapper tableMapper;

//    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加实体", notes = "添加实体")
    @ApiImplicitParam(name = "tableEntity", value = "实体", required = true, dataType = "TableEntity")
    public ResponseMessage insert(@RequestBody @Valid TableEntity tableEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        tableEntity.setId(UUIDUtil.getUUID());
        tableEntity.setCreatedDate(new Date());
        boolean flag = tableService.save(tableEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("保存成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("保存失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PutMapping("/update")
    @ApiOperation(value = "修改实体", notes = "修改实体")
    @ApiImplicitParam(name = "tableEntity", value = "实体", required = true, dataType = "TableEntity")
    public ResponseMessage update(@RequestBody @Valid TableEntity tableEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        tableEntity.setUpdatedDate(new Date());
        boolean flag = tableService.updateById(tableEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "实体删除", notes = "实体删除")
    @ApiImplicitParam(name = "id", value = "实体id")
    public ResponseMessage delete(@RequestParam("id") String id) {

        boolean flag = tableService.removeById(id);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findById")
    @ApiOperation(value = "实体查询", notes = "实体查询")
    @ApiImplicitParam(name = "实体id", value = "实体id")
    public ResponseMessage findById(@RequestParam("id") String id) {
        TableEntity entity = tableService.getById(id);
        return ResponseMessage.defaultResponse().setData(entity);
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "实体分页列表", notes = "实体分页列表")
    @GetMapping(value = "/page")
    public ResponseMessage findByPage(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      HttpServletRequest request) {
        Map<String, Object> filters = RequestUtil.getParameters(request);
        LambdaQueryWrapper<TableEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<TableEntity> tablePage = new Page<>(pageNum , size , true);
        IPage<TableEntity> tableIPage = tableMapper.selectPage(tablePage , userLambdaQueryWrapper);
        return ResponseMessage.defaultResponse().setData(tableIPage);
    }


    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findAllTable")
    @ApiOperation(value = "查询所有实体数据", notes = "查询所有实体数据")
    public ResponseMessage findAllTable() {
        return tableService.findAllTable();
    }

//    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "导出Excel文件", notes = "导出Excel文件", httpMethod = "GET")
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "实体id")
    public void exportExcel(@RequestParam("id") String id, HttpServletResponse response) throws Exception {
        tableService.exportExcel(id, response);
    }


}